package juc;

import java.util.concurrent.TimeUnit;

class HoldLock implements Runnable {
    private String lockA;
    private String lockB;
    // 持 有自已的锁，还想要别人的锁


    public HoldLock(String lockA, String lockB) {
        this.lockA = lockA;
        this.lockB = lockB;
    }

    @Override
    public void run() {
        synchronized (lockA){
            System.out.println("获取得"+lockA);
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (lockB){
                System.out.println("想获取得"+lockB);
            }
        }
    }
}
public class DeadLock {
    public static void main(String[] args) {
        new Thread(new HoldLock("a","b")).start();
        new Thread(new HoldLock("b","a")).start();
        // 怎么解决死锁
        /**
         * 查找死锁
         jps java版的ps jps -l  查看运行的进程编号
         jstack 进程编号  打印栈信息报告
         found 1 deadlock
         找到故障类，然后修改代码
         */
    }
}
